import numpy as np
import csv
import random
from datetime import datetime
from time import strftime

'''
revision of test24
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def cal_region_attri(region_id):
    dis_reg = np.unique(region_id)
    iLen = dis_reg.shape[0]
    temp_reg_attri = np.zeros((iLen, 3)) #[caner, pop, rate]

    # unit_attri = [cancer, pop, area, rate]

    i = 0
    for item in unit_attri:
        temp_reg_attri[int(region_id[i]),0] += item[0]
        temp_reg_attri[int(region_id[i]),1] += item[1]
        #temp_reg_attri[int(region_id[i]),2] + = item[2]
        i = i + 1

    for item in temp_reg_attri:
        item[2] = item[0]/item[1]

    return temp_reg_attri[:,3]

def calListAttri(list, data):
    #  data [ID, cancer, pop]
    tPop = 0
    tCancer = 0
    for item in list:
        tCancer += data[int(item), 1]
        tPop += data[int(item), 2]
    tRate = (tCancer + 0.0)/tPop
    return [tPop, tCancer, tRate]

def cal_riskare_attri(data):
    # data [ID, pop, cancer]
    #temp_popcancer = np.zeros((len(riskID),3))
    temp_popcancer = np.array([])
    temp = calListAttri(H1, data)
    temp_popcancer = np.append(temp_popcancer, temp)
    temp = calListAttri(L2, data)
    temp_popcancer = np.append(temp_popcancer, temp)
    temp = calListAttri(H3, data)
    temp_popcancer = np.append(temp_popcancer, temp)
    temp = calListAttri(H4, data)
    temp_popcancer = np.append(temp_popcancer, temp)
    temp = calListAttri(L5, data)
    temp_popcancer = np.append(temp_popcancer, temp)
    temp = calListAttri(L6, data)
    temp_popcancer = np.append(temp_popcancer, temp)
    temp = calListAttri(H7, data)
    temp_popcancer = np.append(temp_popcancer, temp)
    temp_popcancer.shape = (-1, 3)
    return temp_popcancer

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()

    H1 = [8,16,844,915,919,921,923,924]
    L2 = [5,103,106,513,517,518,520,531,534,535,536,541]
    H3 = [63,265,267,268,333,336,337,339,340,342,343,348]
    H4 = [13,174,178,198,886,887,888,889,890]
    L5 = [146,171,182,810,811,814,815,864,867]
    L6 = [20,133,692,694,695,696,698,702,705]
    H7 = [69,70,87,88,369,370,372,442,443]
    
    unitCSV = 'C:/_DATA/CancerData/test/Jun08/TP1000_1m_16_04.csv'
    dataMatrix = build_data_list(unitCSV)  # [ID, pop, riskID, cancer1, cancer2, cancer3]
    iLen = dataMatrix.shape
    unit_attri = np.zeros((iLen[0],3))  #[ID, cancer, pop]
    unit_attri[:,0] = dataMatrix[:,2]
    unit_attri[:,2] = dataMatrix[:,1]
    
    #riskID = np.unique(unit_attri[:,0])
    #print unit_attri
    #repeat = 1
    #riskarea_attri = np.array([])
    cancerID = 117
    unit_attri[:,1] = dataMatrix[:,cancerID+3] # [riskID, cancer, pop]
    riskarea_attri = cal_riskare_attri(unit_attri)  # [total_cancer, total_pop, total_rate]
    #repeat = repeat + 1
    #riskarea_attri = np.append(riskarea_attri, temp_riskarea_attri)
    #riskarea_attri.shape = (-1,len(riskID)*3)
    #print riskarea_attri
    #riskarea_attri = np.transpose(riskarea_attri)
    print cancerID, riskarea_attri
    
    #filePath = unitCSV[:-4] + "_riskarea_attri.csv"
    #np.savetxt(filePath, riskarea_attri, delimiter=',')
    filePath = 'c:/temp.csv'
    np.savetxt(filePath, riskarea_attri, delimiter=',', fmt = '%10.5f')
    
    print "end at " + getCurTime()
    print "========================================================================"  

           
